﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Collections.ObjectModel;

using AtletIS.Shared.Forms;
using AtletIS.Shared.Forms.Admin;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Forms
{
    public partial class frmMain : Form
    {

        #region Form

        // Constructor
        public frmMain()
        {
            InitializeComponent();
        }

        // Form Load
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                this.taResultCodes.Fill(this.aDbDataSet.ResultCodes);
                DatabaseHelper.FillCompetitionsActive(this.aDbDataSet.Competitions);
                this.competitionsComboBox_SelectedIndexChanged(null, null);

                this.Icon = Properties.Resources.AtletIS64;
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        // Form Close
        bool bConfirmed = false;
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bConfirmed)
            {
                if (MessageBox.Show("Naozaj chcete aplikáciu ukončiť?", "AtletIS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                {
                    bConfirmed = true;
                    Application.Exit();
                }
            }
        }

        #endregion

        #region Left Column

        // Competitions ComboBox selected index
        private void competitionsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (competitionsComboBox.SelectedItem != null)
            {
                DataRowView drvCompetition = (DataRowView)competitionsComboBox.SelectedItem;
                Globals.ActiveCompetition = Helpers.CompetitionHelper.ConvertToEntity(drvCompetition.Row);

                tsslCompetition.Text = "Preteky: " + Globals.ActiveCompetition.Name;
                tsslDiscipline.Text = "Disciplína: N/A";

                // Read discipline list for selected competition
                DisciplineListHelper.FillTreeView(this.tvDisciplines);

                if (tvDisciplines.Nodes.Count > 0)
                {
                    tvDisciplines.SelectedNode = tvDisciplines.Nodes[0];
                    tvDisciplines_AfterSelect(null, null);
                }
            }
        }

        // After discipline was selected in TreeView
        private void tvDisciplines_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                int selectedDisciplineId = Convert.ToInt32(tvDisciplines.SelectedNode.Name);
                
                // Read performances for selected discipline
                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, selectedDisciplineId);

                // Update active discipline
                Globals.ActiveDiscipline = DisciplineListHelper.GetActiveDiscipline(selectedDisciplineId); 

                this.tslDisciplineTitle.Text = tvDisciplines.SelectedNode.FullPath;
                this.tsslDiscipline.Text = "Disciplína: " + tvDisciplines.SelectedNode.FullPath + " (" + tvDisciplines.SelectedNode.Name + ")";
                this.tsslPocetPretekarov.Text = "Počet pretekárov: " + this.aDbDataSet.Performances.Rows.Count.ToString();
            }
        }

        // Selects Node on left mouse click
        private void tvDisciplines_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.tvDisciplines.SelectedNode = e.Node;
            this.tvDisciplines_AfterSelect(null, null);
        }

        // Refresh zoznamu disciplin
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.competitionsComboBox_SelectedIndexChanged(null, null);
        }

        #endregion

        #region Chips Export

        // For discipline radio
        private void rbDiscipline_CheckedChanged(object sender, EventArgs e)
        {
            this.txtCheckPointId.Enabled = this.rbCheckPoint.Checked;
            this.txtStartNum.Enabled = this.rbStartNumber.Checked;
        }
        
        // For check-point radio
        private void rbCheckPoint_CheckedChanged(object sender, EventArgs e)
        {
            this.txtCheckPointId.Enabled = this.rbCheckPoint.Checked;
        }

        // For start number radio
        private void rbStartNumber_CheckedChanged(object sender, EventArgs e)
        {
            this.txtStartNum.Enabled = this.rbStartNumber.Checked;
        }

        // Generate chip results
        private void btnChipStartNumResult_Click(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline == null)
            {
                MessageBox.Show("Vyberte disciplínu, prosím", "Chip Results", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Cursor.Current = Cursors.WaitCursor;

            if (rbDiscipline.Checked)
            {
                ChipResultHelper.Discipline(Globals.ActiveDiscipline, this.chkChipDateTime.Checked, this.chkCleanChipDateTime.Checked, true);
            }

            if (rbCheckPoint.Checked)
            {
                ChipResultHelper.CheckPoint(Globals.ActiveDiscipline, Convert.ToInt32(txtCheckPointId.Text), this.chkChipDateTime.Checked, this.chkCleanChipDateTime.Checked);
            }

            if (rbStartNumber.Checked)
            {
                ChipResultHelper.StartNumber(Globals.ActiveDiscipline, Convert.ToInt32(txtStartNum.Text), this.chkChipDateTime.Checked, this.chkCleanChipDateTime.Checked);
            }

            Cursor.Current = Cursors.Default;
        }

        // Generate chip splits table
        private void btnChipSplitsTable_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ChipResultHelper.Splits(Globals.ActiveDiscipline, false);
            Cursor.Current = Cursors.Default;
        }

        // Generate diff. chip splits table
        private void btnChipSplitsTableDiff_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ChipResultHelper.Splits(Globals.ActiveDiscipline, true);
            Cursor.Current = Cursors.Default;
        }

        #endregion

        #region Chips Functions

        // Import chip data to performances - ChipDateTime to Result
        private void btnChipImport_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Naozaj chcete spustiť import výkonov pre " + Globals.ActiveDiscipline.Name + "?";
                
                if (MessageBox.Show(msg, "Chipy - import výkonov", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;

                Cursor.Current = Cursors.WaitCursor;

                ChipResultHelper.ImportFinish(Globals.ActiveDiscipline, false);

                // Reload performances
                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Globals.ActiveDiscipline.ID);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Import chip data to performances - CleanChipDateTime to Result
        private void btnChipImportClean_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Naozaj chcete spustiť import výkonov pre " + Globals.ActiveDiscipline.Name + ", čistý čas?";
                
                if (MessageBox.Show(msg, "Chipy - import výkonov", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;

                Cursor.Current = Cursors.WaitCursor;

                ChipResultHelper.ImportFinish(Globals.ActiveDiscipline, true);

                // Reload performances
                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Globals.ActiveDiscipline.ID);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Import chip data to performances - AbsoluteChipDateTime to RealStartTime
        private void btnChipImportStart_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Naozaj chcete spustiť import časov štartu pre " + Globals.ActiveDiscipline.Name + "?";
                
                if (MessageBox.Show(msg, "Chipy - import časov štartu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;

                Cursor.Current = Cursors.WaitCursor;

                ChipResultHelper.ImportStart(Globals.ActiveDiscipline);

                // Reload performances
                DatabaseHelper.FillPerformancesByDisciplineListId(this.aDbDataSet.Performances, Globals.ActiveDiscipline.ID);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Chip correction
        private void btnChipCorrections_Click(object sender, EventArgs e)
        {
            frmChipCorrections frm = new frmChipCorrections(0);
            frm.ShowDialog();
        }

        // PreChipData
        private void btnPreChipData_Click(object sender, EventArgs e)
        {
            frmPreChipData frm = new frmPreChipData(Globals.ActiveDiscipline);
            frm.ShowDialog();
        }

        // SMS csv
        private void btnSms_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ChipSmsHelper.CreateCsv(Globals.ActiveDiscipline, this.chkChipDateTime.Checked, this.chkCleanChipDateTime.Checked);
            Cursor.Current = Cursors.Default;
        }

        #endregion

        #region Admin, prihlasky, presuny

        // Administracia - preteky
        private void tsbCompetitions_Click(object sender, EventArgs e)
        {
            frmAdminCompetitions frmComp = new frmAdminCompetitions();
            frmComp.ShowDialog();

            // Reload form
            frmMain_Load(null, null);
        }

        // Administracia - kategorie
        private void tsbCategories_Click(object sender, EventArgs e)
        {
            frmAdminCategories frmCat = new frmAdminCategories();
            frmCat.ShowDialog();
        }

        // Administracia - discipliny
        private void tsbDisciplines_Click(object sender, EventArgs e)
        {
            frmAdminDisciplines frmDisc = new frmAdminDisciplines();
            frmDisc.ShowDialog();
        }

        // Administracia - krajiny
        private void tsbCountries_Click(object sender, EventArgs e)
        {
            frmAdminCountries frmCoun = new frmAdminCountries();
            frmCoun.ShowDialog();
        }

        // Administracia - kluby
        private void tsbClubs_Click(object sender, EventArgs e)
        {
            frmAdminClubs frmClu = new frmAdminClubs();
            frmClu.ShowDialog();
        }

        // Administracia - pretekari
        private void tsbCompetitors_Click(object sender, EventArgs e)
        {
            frmAdminCompetitors frmComp = new frmAdminCompetitors();
            frmComp.ShowDialog();
        }

        // Administracia - Chip Tags
        private void tsbChipTags_Click(object sender, EventArgs e)
        {
            frmAdminChipTagsGroups frm = new frmAdminChipTagsGroups();
            frm.ShowDialog();
        }

        // Otvori Prihlasky
        private void tsbPrihlasky_Click(object sender, EventArgs e)
        {
            Signups.frmSignups mySignupsForm = new Signups.frmSignups();
            mySignupsForm.ShowDialog();

            this.tvDisciplines_AfterSelect(null, null);
        }

        // Otvori Presuny
        private void tsbPresuny_Click(object sender, EventArgs e)
        {
            frmTransfer myFrmPresuny = new frmTransfer();
            myFrmPresuny.ShowDialog();

            this.tvDisciplines_AfterSelect(null, null);
        }

        #endregion

        #region Performances grid

        /// <summary>
        /// Pridá nový záznam do Performances pre aktívnu disciplínu.
        /// </summary>
        /// <remarks>Nastaví ID aktívnej disciplíny a ID krajiny na SVK.</remarks>
        private void tsbNewPerformance_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                AtletIS.Shared.Data.aDbDataSet.PerformancesRow drPerformance = this.aDbDataSet.Performances.NewPerformancesRow(); 

                drPerformance.DisciplineListId = Globals.ActiveDiscipline.ID;
                drPerformance.CompetitorId = 1; // FIKT
                drPerformance.FirstName = string.Empty;
                drPerformance.Surname = string.Empty;
                drPerformance.Birthdate = new DateTime(1990, 1, 1);
                drPerformance.Club = string.Empty;
                drPerformance.CountryId = "SVK";
                drPerformance.SMS = string.Empty;
                drPerformance.StartNumber = 0;
                drPerformance.Lane = 0;
                drPerformance.Code = 0;
                drPerformance.SB = 0;
                drPerformance.PB = 0;
                drPerformance.Result = 0;
                drPerformance.ResultQuality = string.Empty;
                drPerformance.AdvResult = 0;
                drPerformance.Rank = 0;
                drPerformance.SecondaryRank = 0;
                drPerformance.Reaction = string.Empty;
                drPerformance.Points = 0;
                drPerformance.PointsScoring = true;
                drPerformance.Active = true;
                drPerformance.Sync = false;
                drPerformance.WebId = 0;
                drPerformance.Modified = DateTime.Now;
                drPerformance.SetRealStartTimeNull();

                this.aDbDataSet.Performances.AddPerformancesRow(drPerformance);
                this.taPerformances.Update(this.aDbDataSet.Performances);

                this.tvDisciplines_AfterSelect(null, null);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Zmazat
        private void tsbDeletePerformance_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.bsPerformances.EndEdit();

                if (this.performancesDataGridView.CurrentRow == null)
                    return;

                if (MessageBox.Show("Naozaj chcete zmazať vybraný záznam?", "Zmazať záznam", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    
                    int performanceID = Convert.ToInt32(((DataRowView)this.performancesDataGridView.CurrentRow.DataBoundItem)["ID"]);
                    
                    DataTable dtDel = DatabaseHelper.GetPerformancesById(performanceID);
                    if (dtDel != null && dtDel.Rows.Count > 0)
                    {
                        try
                        {
                            dtDel.Rows[0]["Active"] = false;
                            this.taPerformances.Update(dtDel.Rows[0]);

                            this.tvDisciplines_AfterSelect(null, null);
                        }
                        catch
                        {
                            MessageBox.Show("Záznam nemôže byť zmazaný!", "Zmazať záznam", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Ulozi zmeny
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                this.Validate();
                this.bsPerformances.EndEdit();
                this.taPerformances.Update(this.aDbDataSet.Performances);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Zoradi, ulozi zmeny
        private void tsbSortSave_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                
                if (tvDisciplines.SelectedNode.Level == 0 | tvDisciplines.SelectedNode.Level == 2)
                {
                    this.Validate();
                    this.bsPerformances.EndEdit();
                    this.taPerformances.Update(this.aDbDataSet);

                    switch (Globals.ActiveDiscipline.DisciplineCode)
                    {
                        case Globals.SprintCode:
                        case Globals.LongRunCode:
                        case Globals.MarathonCode:
                            ResultsHelper.UpdatePerformances(Globals.ActiveDiscipline);
                            break;

                        case Globals.ThrowsCode:
                        case Globals.HorJumpCode:
                        case Globals.VerJumpCode:
                            ExtTextsHelper.UpdatePerformances(Globals.ActiveDiscipline);
                            break;
                    }

                    this.tvDisciplines_AfterSelect(null, null);
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        #endregion

        #region Disciplines Context Menu

        private void cmsDisciplines_Opening(object sender, CancelEventArgs e)
        {
            if (tvDisciplines.SelectedNode == null)
                return;
            
            // Parent discipline
            if (tvDisciplines.SelectedNode.Level == 0)
            {
                this.tsmiSplitAuto.Enabled = true;
                this.tsmiSplitCancel.Enabled = false;
                this.tsmiAdvance.Enabled = false;
                this.tsmiAdvanceCancel.Enabled = false;

                this.tsmiSubdisciplineCreate.Enabled = true;
                this.tsmiSubdisciplineDelete.Enabled = false;

                tsmiPremenovat.Enabled = true;
                tsmiBodovat.Enabled = true;
            }

            // Child discipline
            if (tvDisciplines.SelectedNode.Level == 1)
            {
                this.tsmiSplitAuto.Enabled = false;
                this.tsmiSplitCancel.Enabled = true;
                this.tsmiAdvance.Enabled = true;
                this.tsmiAdvanceCancel.Enabled = true;

                this.tsmiSubdisciplineCreate.Enabled = true;
                this.tsmiSubdisciplineDelete.Enabled = true;
                
                tsmiPremenovat.Enabled = true;
                tsmiBodovat.Enabled = true;
            }

            // Grand child discipline
            if (tvDisciplines.SelectedNode.Level == 2)
            {
                this.tsmiSplitAuto.Enabled = false;
                this.tsmiSplitCancel.Enabled = false;
                this.tsmiAdvance.Enabled = false;
                this.tsmiAdvanceCancel.Enabled = false;

                this.tsmiSubdisciplineCreate.Enabled = false;
                this.tsmiSubdisciplineDelete.Enabled = true;
                
                tsmiPremenovat.Enabled = true;
                tsmiBodovat.Enabled = false;
            }

        }

        #region Discipline Export

        // Startovna listina
        private void tsmiStartList_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode == null)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (tvDisciplines.SelectedNode.Level == 0 | tvDisciplines.SelectedNode.Level == 2)
                Helpers.StartListHelper.SingleStartList(Globals.ActiveDiscipline);

            if (tvDisciplines.SelectedNode.Level == 1)
                Helpers.StartListHelper.MultiStartList(tvDisciplines.SelectedNode, Globals.ActiveDiscipline);

            Cursor.Current = Cursors.Default;
        }

        // Vysledkova listina
        private void tsmiResults_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode == null)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (tvDisciplines.SelectedNode.Level == 0 | tvDisciplines.SelectedNode.Level == 2)
            {
                switch (Globals.ActiveDiscipline.DisciplineCode)
                {
                    case Globals.SprintCode:
                    case Globals.LongRunCode:
                    case Globals.MarathonCode:
                    case Globals.Triathlon:
                        ResultsHelper.SingleRun(Globals.ActiveDiscipline);
                        break;

                    case Globals.ThrowsCode:
                    case Globals.HorJumpCode:
                    case Globals.VerJumpCode:
                        ResultsHelper.FieldResults(Globals.ActiveDiscipline);
                        break;

                    case Globals.PointsAsc:
                    case Globals.PointsDesc:
                        ResultsHelper.Points(Globals.ActiveDiscipline);
                        break;
                }
            }

            if (tvDisciplines.SelectedNode.Level == 1)
            {
                switch (Globals.ActiveDiscipline.DisciplineCode)
                {
                    case Globals.SprintCode:
                    case Globals.LongRunCode:
                        ResultsHelper.MultiRun(tvDisciplines.SelectedNode);
                        break;
                }
            }

            Cursor.Current = Cursors.Default;
        }

        // Zapis pre rozhodcov
        private void tsmiRefereeList_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            RefereeListHelper.GetRefereeList(Globals.ActiveDiscipline);
            Cursor.Current = Cursors.Default;
        }

        #endregion

        // Import z MacFinish
        private void tsmiMFImport_Click(object sender, EventArgs e)
        {
            frmMacFinishImport myFrmMacfinishImport = new frmMacFinishImport(Globals.ActiveDiscipline);
            myFrmMacfinishImport.ShowDialog();

            this.tvDisciplines_AfterSelect(null, null);
        }

        #region Applications

        private void tsmiSpeaker_Click_1(object sender, EventArgs e)
        {
            frmSpeaker mySpeakerForm = new frmSpeaker(Globals.ActiveDiscipline, tvDisciplines.SelectedNode);
            mySpeakerForm.Show();
        }

        private void tsmiSektor_Click_1(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.DisciplineCode == 3 | Globals.ActiveDiscipline.DisciplineCode == 4 | Globals.ActiveDiscipline.DisciplineCode == 5)
            {
                frmSector mySectorForm = new frmSector(Globals.ActiveDiscipline);
                mySectorForm.ShowDialog();

                this.tvDisciplines_AfterSelect(null, null);
            }
            else
                MessageBox.Show("Pre túto disciplínu nemožno otvoriť Sektor.", "Sektor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void tsmiWindspeed_Click(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.DisciplineCode == 4)
            {
                frmWindSpeed myWindSpeedForm = new frmWindSpeed(Globals.ActiveDiscipline);
                myWindSpeedForm.ShowDialog();
            }
            else
                MessageBox.Show("Pre túto disciplínu nie je možné otvoriť Vetromer.", "Vetromer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void tsmiTrack_Click_1(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.DisciplineCode == Globals.SprintCode
                | Globals.ActiveDiscipline.DisciplineCode == Globals.LongRunCode
                | Globals.ActiveDiscipline.DisciplineCode == Globals.MarathonCode)
            {
                frmTrack myFrmTrack = new frmTrack(Globals.ActiveDiscipline);
                myFrmTrack.ShowDialog();
            }
            else
                MessageBox.Show("Pre túto disciplínu nie je možné otvoriť Track.", "Track", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void tsmiChipViewerWpf_Click_1(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.DisciplineCode == Globals.LongRunCode
                | Globals.ActiveDiscipline.DisciplineCode == Globals.MarathonCode)
            {
                frmWpf myFrmWpf = new frmWpf("ChipViewer", Globals.ActiveDiscipline);
                myFrmWpf.Show();
            }
            else
                MessageBox.Show("Pre túto disciplínu nie je možné otvoriť Chip Viewer.", "Chip Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void tsmiChipViewerOld_Click(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.DisciplineCode == Globals.LongRunCode
                | Globals.ActiveDiscipline.DisciplineCode == Globals.MarathonCode)
            {
                frmChipViewer myChipViewerForm = new frmChipViewer(Globals.ActiveDiscipline);
                myChipViewerForm.Show();
            }
            else
                MessageBox.Show("Pre túto disciplínu nie je možné otvoriť Chip Viewer.", "Chip Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        #endregion

        #region Graphics

        private void tsmiStartListGr_Click(object sender, EventArgs e)
        {
            frmWpf myFrmWpf = new frmWpf("StartList", Globals.ActiveDiscipline);
            myFrmWpf.Show();
        }

        private void tsmiResultsGr_Click(object sender, EventArgs e)
        {
            frmWpf myFrmWpf = new frmWpf("Results", Globals.ActiveDiscipline);
            myFrmWpf.Show();
        }

        #endregion

        // Automaticke rozdelenie
        private void tsmiSplitAuto_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                frmSplitAuto myFrmSplitAuto = new frmSplitAuto(tvDisciplines.SelectedNode);
                myFrmSplitAuto.ShowDialog();
                DisciplineListHelper.FillTreeView(tvDisciplines);
            }
        }

        // Zrusit rozdelenie
        private void tsmiZrusitRozdelenie_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                string sName = "[" + tvDisciplines.SelectedNode.Parent.Text + "/" + tvDisciplines.SelectedNode.Text + "]";
                if (MessageBox.Show("Naozaj chcete zrušiť rozdelenie: " + sName + " ?", "Zrušiť rozdelenie", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DisciplineListHelper.CancelSplit(tvDisciplines.SelectedNode);
                    DisciplineListHelper.FillTreeView(tvDisciplines);
                }
            }
        }

        // Postup
        private void tsmiPostup_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                frmAdvance myFrmAdvance = new frmAdvance(tvDisciplines.SelectedNode);
                myFrmAdvance.ShowDialog();
                DisciplineListHelper.FillTreeView(tvDisciplines);
            }
        }

        // Zrusit postup
        private void tsmiAdvanceCancel_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                string sName = "[" + tvDisciplines.SelectedNode.Parent.Text + "/" + tvDisciplines.SelectedNode.Text + "]";
                if (MessageBox.Show("Naozaj chcete zrušiť postupové kolo: " + sName + " ?", "Zrušiť postupové kolo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DisciplineListHelper.CancelAdvance(tvDisciplines.SelectedNode);
                    DisciplineListHelper.FillTreeView(tvDisciplines);
                }
            }
        }

        // Vytvorit sub-disciplinu
        private void tsmiSubdisciplineCreate_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                frmTextInput myFrmTextInput = new frmTextInput(tvDisciplines.SelectedNode);
                myFrmTextInput.status = 1;
                myFrmTextInput.tnDiscipline = tvDisciplines.SelectedNode;
                myFrmTextInput.ShowDialog();

                DisciplineListHelper.FillTreeView(tvDisciplines);
            }
        }

        // Zmazat
        private void tsmiDeleteDiscipline_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                if (MessageBox.Show("Naozaj chcete disciplínu zmazať?", "Zmazanie disciplíny", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    DataTable dtDisciplineList = DatabaseHelper.GetDisciplineListById(Globals.ActiveDiscipline.ID);
                    if (dtDisciplineList != null && dtDisciplineList.Rows.Count > 0)
                    {
                        DataRow dr = dtDisciplineList.Rows[0];
                        dr["Active"] = 0;
                        dr["Modified"] = DateTime.Now;
                        taDisciplineList.Update(dr);

                        DisciplineListHelper.FillTreeView(tvDisciplines);
                    }
                }
            }
        }

        // Premenovat
        private void tsmiPremenovat_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode != null)
            {
                frmTextInput myFrmTextInput = new frmTextInput(tvDisciplines.SelectedNode);
                myFrmTextInput.status = 2;
                myFrmTextInput.tnDiscipline = tvDisciplines.SelectedNode;
                myFrmTextInput.ShowDialog();
                DisciplineListHelper.FillTreeView(tvDisciplines);
            }
        }

        // Ukoncit
        private void tsmiUkoncit_Click(object sender, EventArgs e)
        {
            StatusHelper.UpdateStatus(Globals.ActiveDiscipline, Globals.StatusFinished);
            Globals.ActiveDiscipline.Status = Globals.StatusFinished;
        }

        // Bodovat
        private void tsmiBodovat_Click(object sender, EventArgs e)
        {
            if (Globals.ActiveDiscipline.Status != Globals.StatusFinished)
            {
                MessageBox.Show("Disciplína nie je ukončená, nie je možné ju obodovať.", "Bodovať disciplínu", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (tvDisciplines.SelectedNode.Level == 0)
                PointsHelper.Simple(Globals.ActiveDiscipline);

            if (tvDisciplines.SelectedNode.Level == 1)
                PointsHelper.Multi(Globals.ActiveDiscipline);

            this.tvDisciplines_AfterSelect(null, null);
        }

        // Doplnujuce texty
        private void tsmiExtTexts_Click(object sender, EventArgs e)
        {
            frmExtTexts frm = new frmExtTexts(Globals.ActiveDiscipline.ID, 0);
            frm.ShowDialog();
        }

        // Vlastnosti
        private void tsmiStartTime_Click(object sender, EventArgs e)
        {
            frmStartTime frm = new frmStartTime(Globals.ActiveDiscipline);
            frm.ShowDialog();

            this.tvDisciplines_AfterSelect(null, null);
        }

        // Sekvencne starty
        private void tsmiSeqStartTime_Click(object sender, EventArgs e)
        {
            frmSeqStartTime frm = new frmSeqStartTime(Globals.ActiveDiscipline);
            frm.ShowDialog();

            this.tvDisciplines_AfterSelect(null, null);
        }

        #endregion

        #region Performances Context Menu

        private void tsmiExtTextsPerfs_Click(object sender, EventArgs e)
        {
            if (performancesDataGridView.CurrentRow != null)
            {
                int performanceId = Convert.ToInt32(((DataRowView)performancesDataGridView.CurrentRow.DataBoundItem)["ID"]);
                
                frmExtTexts frm = new frmExtTexts(0, performanceId);
                frm.ShowDialog();
            }
        }

        #endregion

        #region View tab

        private void chkLane_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColLane"].Visible = chkLane.Checked;
        }

        private void chkClub_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColClubId"].Visible = chkClub.Checked;
        }

        private void chkCountry_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColCountryId"].Visible = chkCountry.Checked;
        }

        private void chkPoints_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColPoints"].Visible = chkPoints.Checked;
            performancesDataGridView.Columns["dgvChkColPointsScoring"].Visible = chkPoints.Checked;
        }

        private void chkReaction_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColReaction"].Visible = chkReaction.Checked;
        }

        private void chkSB_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColSB"].Visible = chkSB.Checked;
        }

        private void chkPB_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColPB"].Visible = chkPB.Checked;
        }

        private void chkSMS_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColSMS"].Visible = chkSMS.Checked;
        }

        private void chkAdvResult_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColAdvResult"].Visible = chkAdvResult.Checked;
        }

        private void chkRealStartTime_CheckedChanged(object sender, EventArgs e)
        {
            performancesDataGridView.Columns["dgvTxtColRealStartTime"].Visible = this.chkRealStartTime.Checked;
        }

        #endregion

        #region CompleteExport

        // Zoznam disciplin
        private void tsmiDisciplineList_Click(object sender, EventArgs e)
        {
            DisciplineListHelper.Export(Globals.ActiveCompetition);
        }

        // Celkova statovna listina
        private void tsmiStartListComplete_Click(object sender, EventArgs e)
        {
            frmStandardResults myFrmCompleteResults = new frmStandardResults(1);
            myFrmCompleteResults.ShowDialog();
        }

        // Celkova vysledkova listina
        private void tsmiResultsComplete_Click(object sender, EventArgs e)
        {
            frmStandardResults myFrmCompleteResults = new frmStandardResults(2);
            myFrmCompleteResults.ShowDialog();
        }

        // Absolutne poradie
        private void tsmiTotalResults_Click(object sender, EventArgs e)
        {
            frmTotalResults frm = new frmTotalResults();
            frm.ShowDialog();
        }

        #endregion

    }
}
